package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.utils.PhoneUtils;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/23.
 * 收件信息界面
 */

public class RecipientInfoActivity extends BaseActivity2 implements TextWatcher, View.OnClickListener {

    public static final String RECIPIENT_INFO = "RECIPIENT_INFO";


    private RelativeLayout rl_receive_person;
    private RelativeLayout rl_receive_tel;
    private RelativeLayout rl_receive_address;
    private TextView tv_receive_person;
    private TextView tv_receive_tel;
    private TextView tv_receive_address;
    private Button btn_confirm;
    private Dialog mAddressDialog;
    private String personType = "1";
    private String telType = "2";
    private String addType = "3";
    private String itemType = "";
    private EntityBean invoicebean;
    private EntityBean bean;
    private EntityBean listbean;

    private ArrayList<String> invoiceIds;

    private String receive_person;
    private String receive_tel;
    private String receive_address;

    private TextView tv_confirm;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.acticity_recipient_info;
    }

    @Override
    public void initView() {
        rl_receive_person = (RelativeLayout) findViewById(R.id.rl_receive_person);
        rl_receive_tel= (RelativeLayout) findViewById(R.id.rl_receive_tel);
        rl_receive_address= (RelativeLayout) findViewById(R.id.rl_receive_address);
        tv_receive_person = (TextView) findViewById(R.id.tv_receive_person);
        tv_receive_tel = (TextView) findViewById(R.id.tv_receive_tel);
        tv_receive_address = (TextView) findViewById(R.id.tv_receive_address);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_confirm.setEnabled(false);
        setToolbarTitle(AppUtil.getString(R.string.recipient_info));
        initData();
        initEvent();
    }

    public void initData(){
        Intent intent = getIntent();
        String s = intent.getStringExtra(InvoiceInfoEditActivity.INVOICE_INFO_EDIT);
        EntityBean mBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(s, EntityBean.class);
        invoiceIds = intent.getStringArrayListExtra(InvoiceInfoEditActivity.INVOICE_ID);
        invoicebean = (EntityBean) mBean.get("invoicebean");
        if(!TextUtils.isEmpty(Control.getRecipientName())){
            tv_receive_person.setText(Control.getRecipientName());
        }

        if(!TextUtils.isEmpty(Control.getRecipientTel())){
            tv_receive_tel.setText(Control.getRecipientTel());
        }

        if(!TextUtils.isEmpty(Control.getRecipientAdd())){
            tv_receive_address.setText(Control.getRecipientAdd());
        }

        boolean b = checkText();
        if (b) {
            btn_confirm.setEnabled(true);
        } else {
            btn_confirm.setEnabled(false);
        }
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void initEvent() {
        rl_receive_person.setOnClickListener(this);
        rl_receive_tel.setOnClickListener(this);
        rl_receive_address.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
        tv_receive_person.addTextChangedListener(this);
        tv_receive_tel.addTextChangedListener(this);
        tv_receive_address.addTextChangedListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_receive_person:
                itemType = personType;
                showAddressDialog(itemType);
                break;

            case R.id.rl_receive_tel:
                itemType = telType;
                showAddressDialog(itemType);
                break;

            case R.id.rl_receive_address:
                itemType = addType;
                showAddressDialog(itemType);
                break;

            //点击确定提交跳转到确认信息界面
            case R.id.btn_confirm:
                setData2Bean();
                openConfirmInfo(bean);
                break;
        }
    }

    public void setData2Bean(){
        //收件人
        String receive_person = tv_receive_person.getText().toString();
        //联系电话
        String receive_tel = tv_receive_tel.getText().toString();
        //收件地址
        String receive_address = tv_receive_address.getText().toString();
        invoicebean.set("addressee",receive_person);
        invoicebean.set("mobile",receive_tel);
        invoicebean.set("sendaddress",receive_address);
        bean =new EntityBean();
//        bean.set("chargelistids",listbean);
        bean.set("invoicebean",invoicebean);
    }

    public void showAddressDialog(final String type){

        final View mEditDialog = View.inflate(mContext, R.layout.dialog_invoice_edit, null);
        final TextView tv_dialog_title= (TextView) mEditDialog.findViewById(R.id.tv_dialog_title);
        final TextView tv_dialog_notice= (TextView) mEditDialog.findViewById(R.id.tv_dialog_notice);
        final EditText et_dialog_info= (EditText) mEditDialog.findViewById(R.id.et_dialog_info);
        TextView tv_cancel = (TextView) mEditDialog.findViewById(R.id.tv_cancel);
        tv_confirm = (TextView) mEditDialog.findViewById(R.id.tv_confirm);
        final LinearLayout ll_recipient_content  = (LinearLayout)mEditDialog.findViewById(R.id.ll_recipient_content);
        final LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ll_recipient_content.getLayoutParams();
        if( type == personType){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.recipient_person));
            tv_dialog_notice.setVisibility(View.GONE);
            mAddressDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            if(!TextUtils.isEmpty(Control.getRecipientName())){
                et_dialog_info.setText(Control.getRecipientName());
                et_dialog_info.setSelection(Control.getRecipientName().length());
            }
        }else if( type == telType ){
            layoutParams.height = AppUtil.dip2px(124);
            ll_recipient_content.setLayoutParams(layoutParams);
            tv_dialog_title.setText(AppUtil.getString(R.string.recipient_tel));
            tv_dialog_notice.setVisibility(View.GONE);
            mAddressDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 169);
            et_dialog_info.setInputType(InputType.TYPE_CLASS_NUMBER);
            if(!TextUtils.isEmpty(Control.getRecipientTel())){
                et_dialog_info.setText(Control.getRecipientTel());
                et_dialog_info.setSelection(Control.getRecipientTel().length());
            }
        }else if(type == addType){
            tv_dialog_title.setText(AppUtil.getString(R.string.recipient_address));
            tv_dialog_notice.setText(AppUtil.getString(R.string.address_notice));
            mAddressDialog = DialogUtil.getInstance().creatAlertDialog(mContext, mEditDialog, 311, 211);
            if(!TextUtils.isEmpty(Control.getRecipientAdd())){
                et_dialog_info.setText(Control.getRecipientAdd());
                et_dialog_info.setSelection(Control.getRecipientAdd().length());
            }
        }
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String dialigInfo = et_dialog_info.getText().toString();
                if(!TextUtils.isEmpty(dialigInfo)){
                    if( type == personType){
                        tv_receive_person.setText(dialigInfo);
                        Control.setRecipientName(tv_receive_person.getText().toString());
                        DialogUtil.getInstance().dismiss();
                    }else if( type == telType ){
                        if (PhoneUtils.isPhoneNumber(et_dialog_info.getText().toString())||PhoneUtils.isTelPhoneNumber(et_dialog_info.getText().toString())) {
                            tv_receive_tel.setText(dialigInfo);
                            Control.setRecipientTel(tv_receive_tel.getText().toString());
                            DialogUtil.getInstance().dismiss();
                        }else {
                            showToast(getString(R.string.tel_hint));
                        }
                    }else if(type == addType){
                        tv_receive_address.setText(dialigInfo);
                        Control.setRecipientAdd(tv_receive_address.getText().toString());
                        DialogUtil.getInstance().dismiss();
                    }
                }

            }
        });

        et_dialog_info.addTextChangedListener(this);
        mAddressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        mAddressDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        boolean b = checkText();
        if (b) {
            btn_confirm.setEnabled(true);
        } else {
            btn_confirm.setEnabled(false);
        }

        if(itemType == personType||itemType == telType||itemType == addType){
            tv_confirm.setTextColor(Color.parseColor("#FF007AFF"));
        }
    }

    /**
     * 检查是否全部填写
     *
     * @return 全部有填写 返回true
     */
    private boolean checkText() {
        //收件人
        receive_person = tv_receive_person.getText().toString();
        PrintLog.e("Recipient",receive_person);
        if (TextUtils.isEmpty(receive_person)) {
            return false;
        }
        //联系电话
        receive_tel = tv_receive_tel.getText().toString();
        if (TextUtils.isEmpty(receive_tel)) {
            return false;
        }

        //收件地址
        receive_address = tv_receive_address.getText().toString();
        if (TextUtils.isEmpty(receive_address)) {
            return false;
        }
        return true;
    }

    //打开确认信息界面
    public void openConfirmInfo(EntityBean bean){
        Intent intent =new Intent(this,InvoiceConfirmActivity.class);
        intent.putStringArrayListExtra(InvoiceConfirmActivity.INVOICE_ID,invoiceIds);
        String entityBean = JSONSerializer.getInstance().Serialize(bean);
        intent.putExtra(RECIPIENT_INFO,entityBean);
        startActivity(intent);
    }
}
